//
//  TestCollectionViewCell.m
//  TestCollectionView
//
//  Created by sunshinfight@163.com on 2019/4/17.
//  Copyright © 2019 sunshinfight@163.com. All rights reserved.
//

#import "JXCalendarItemCell.h"

@implementation JXCalendarItemCell{
    UIColor *_selectedColor;
    UIColor *_currentDayBordColor;
    JXCalendarModel *_model;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _selectedColor = [UIColor colorWithRed:243.0/255.0 green:121.0/255.0 blue:56.0/255.0 alpha:1.0];
        _currentDayBordColor = [UIColor colorWithRed:217.0/255.0 green:217.0/255.0 blue:217.0/255.0 alpha:1.0];
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    self.clipsToBounds = YES;
    [self.contentView addSubview:self.dateLabel];
    self.dateLabel.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *left = [NSLayoutConstraint constraintWithItem:self.dateLabel attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:7.5];
    NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.dateLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    NSLayoutConstraint *right = [NSLayoutConstraint constraintWithItem:self.dateLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-7.5];
    NSLayoutConstraint *bottom = [NSLayoutConstraint constraintWithItem:self.dateLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self.contentView addConstraints: @[left, right, top, bottom]];
}

- (UILabel *)dateLabel
{
    if (_dateLabel == nil) {
        _dateLabel = [UILabel new];
        _dateLabel.numberOfLines = 2;
        _dateLabel.textAlignment = NSTextAlignmentCenter;
        _dateLabel.backgroundColor = UIColor.clearColor;
        _dateLabel.layer.borderWidth = 0.5;
        _dateLabel.textColor = [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
        _dateLabel.font = [UIFont systemFontOfSize:13];
    }

    return _dateLabel;
}

- (void)configCellWithModel: (JXCalendarModel*)model
{
    _model = model;
    NSString *datestring = model.isToday ? [NSString stringWithFormat:@"%d\n今天", model.day] : [NSString stringWithFormat:@"%d", model.day];
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    paragraphStyle.lineSpacing = 5;
    NSDictionary *attriDic = @{NSFontAttributeName : [UIFont systemFontOfSize:13], NSParagraphStyleAttributeName : paragraphStyle};
    NSMutableAttributedString *dateAttrString = [[NSMutableAttributedString alloc] initWithString:datestring
                                                                                       attributes:attriDic];
    self.dateLabel.attributedText = dateAttrString;
    
    self.dateLabel.layer.borderColor = model.isToday ? _currentDayBordColor.CGColor : UIColor.clearColor.CGColor;
    [self setSelected:model.isSelected];
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    self.dateLabel.textColor = selected ? UIColor.whiteColor : [UIColor colorWithRed:51.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:1.0];
    self.dateLabel.backgroundColor = selected ? _selectedColor : UIColor.whiteColor;
    if (selected) {
        self.dateLabel.layer.borderColor = UIColor.clearColor.CGColor;
    } else {
        self.dateLabel.layer.borderColor = _model.isToday ? _currentDayBordColor.CGColor : UIColor.clearColor.CGColor;
    }
}

@end
